package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportUser;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportUserDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-05-18 20:14:42
 */
public class TransportUserConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportUserConverts.class);

    public static List<TransportUserDTO> convert(List<TransportUser> transportUserList) {
        if (CollectionUtils.isEmpty(transportUserList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportUserDTO> transportUserListResult = Lists.newArrayList();
        for (TransportUser transportUser : transportUserList) {
            transportUserListResult.add(convert(transportUser));
        }
        return transportUserListResult;
    }

    public static TransportUserDTO convert(TransportUser transportUser) {
        try {
            return BeanUtils.copy(transportUser, TransportUserDTO.class);
        } catch (Exception e) {
            LOG.error("TransportUser到TransportUserDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportUser到TransportUserDTO转换失败", e);
        }
    }

    public static TransportUser convert(TransportUserDTO transportUserDTO) {
        try {
            return BeanUtils.copy(transportUserDTO, TransportUser.class);
        } catch (Exception e) {
            LOG.error("TransportUserDTO到TransportUser转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportUserDTO到TransportUser转换失败", e);
        }
    }
}